﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using CMN.Extends;
using System.IO;
using CMN.Framework;
using System.Data.Common;
using CMN.DBUtility;
using CMN.Framework;
namespace CMN.Extends.Job
{
    public class JobGetMoudleList : IJob
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void Execute(object obj)
        {
            
            List<Normal_ClickRecordEntity> clickRecord = MvcWebCacheManager.GetNormal_ClickRecordList();
            if (clickRecord == null)
             return;

            try
            {

                Normal_ClickRecordEntity[] clickRecordNew = new Normal_ClickRecordEntity[clickRecord.Count];
                clickRecord.CopyTo(clickRecordNew, 0);
                clickRecord.Clear();
                
               
                //AppLog.Write("更新点击数" + clickRecord.Count.ToString(), AppLog.LogMessageType.Debug);
                MvcWebCacheManager.RemoveNormal_ClickRecordList();
            }
            catch (Exception ex)
            {
                AppLog.Write("记录点击数写入数据库报错" + ex.Message, AppLog.LogMessageType.Error);
            }
            finally
            {
                clickRecord = null;
            }

               
        }

        private void saveClick(Normal_ClickRecordEntity[] clickRecord)
        {
            if (clickRecord == null)
                return;
            List<CommandInfo> comdList = new List<CommandInfo>();
            Normal_ChannelEntity channelEntity = null;
            BLLHelper bll = new BLLHelper();
            for (int i = 0; i < clickRecord.Length; i++)
            {
                channelEntity = MvcWebCacheManager.GetNormal_ChannelByPath(clickRecord[i].ModuleType);
                if (channelEntity != null)
                {
                    CommandInfo info = new CommandInfo();
                    info.CommandNextType = CommandNextType.ExcuteEffectRows;
                    info.CommandText = "Insert into Normal_ClickRecord ( Ip,AddDate,ModuleType,id )VALUES(@Ip,@AddDate,@ModuleType,@Id)";
                    info.CommandType = System.Data.CommandType.Text;
                    DbParameter[] prams;
                    prams = new DbParameter[4];
                    prams[0] = bll.GetDalHelper().CreateInDbParameter("Ip", MyDBType.Varchar, clickRecord[i].Ip);//IP地址 
                    prams[1] = bll.GetDalHelper().CreateInDbParameter("AddDate", MyDBType.DateTime, clickRecord[i].AddDate);//添加时间 
                    prams[2] = bll.GetDalHelper().CreateInDbParameter("ModuleType", MyDBType.Varchar, clickRecord[i].ModuleType);//文章类型 
                    prams[3] = bll.GetDalHelper().CreateInDbParameter("Id", MyDBType.Int, clickRecord[i].Id);//文章Id 
                    info.Parameters = prams;
                    comdList.Add(info);
                    CommandInfo info2 = new CommandInfo();
                    info2.CommandText = string.Concat("update ", "Module_", channelEntity.ChannelType, " set ViewNum=ViewNum+1  where id=", clickRecord[i].Id.ToString());
                    info2.CommandType = System.Data.CommandType.Text;
                    info2.CommandNextType = CommandNextType.None;
                    info2.Parameters = null;
                    comdList.Add(info2);
                }

            }
            if (comdList.Count > 0)
                bll.GetDalHelper().ExecuteSqlTran(comdList);
        }

        public void Error(Exception e)
        {
            AppLog.Write("采集列表出错", AppLog.LogMessageType.Error, e);
        }
    }
}
